'DATE: Oct 15, 2012
'AUTHOR: Richard Frank
'CMPT 110 - Assignment #2



'This program reads a file in for some purchases, calculates the total cost of the purchases and displays them to the listbox
'YOU SHOULD WRITE MUCH MORE DETAIL HERE ON WHAT YOUR PROGRAM DOES


Public Class Till

    Private Sub CalculateTotal(sender As System.Object, e As System.EventArgs) Handles btnCalculateTotal.Click
        'This procedure reads the file in, and for each purchase, cleans the input and calculates the total
        'if an error is detected, then the program shows an error message to the user

        'this is how my items are formatted
        Dim fmtStr As String = "{0, -27}{1, 15}{2, 15}{3, 18}{4, 15}{5, 18}"

        'write the headers
        lstOutput.Items.Add(String.Format(fmtStr, "Item", "Unit Price", "Quantity", "Sub Total", "Tax", "Total Cost"))

        Dim readerVar As IO.StreamReader

        'Click on 'Project' -> 'Add New Item' -> 'Text File' -> name the file 'input.txt'
        ''input.txt' will be added to the Solution Explorer window. Click on it.
        'on the 'Properties' window change 'Copy to output directory' to 'Copy Always'
        readerVar = IO.File.OpenText("input.txt")

        'use this to count which input line number we're on
        Dim intLineNumber As Integer = 0

        'read until I run out of lines
        While readerVar.Peek <> -1

            intLineNumber = intLineNumber + 1

            'read all the components of the price, assuming all lines exist but might have missing characters
            'assume items might not numbers, or $s might be missing, or % might not be there
            Dim strItem As String = readerVar.ReadLine
            Dim strPrice As String = readerVar.ReadLine
            Dim strQuantity As String = readerVar.ReadLine
            Dim strTaxRate As String = readerVar.ReadLine

            'clean up the input
            strPrice = strPrice.Replace("$", "")
            strTaxRate = strTaxRate.Replace("%", "")

            'test to make sure we have proper values
            If IsNumeric(strPrice) And IsNumeric(strQuantity) And IsNumeric(strTaxRate) Then

                'convert to the proper format
                Dim sngPrice As Single = CSng(strPrice)
                Dim intQuantity As Integer = CInt(strQuantity)
                Dim sngTaxRate As Single = CSng(strTaxRate) / 100

                Dim sngSubTotal As Single = sngPrice * intQuantity      'calculate subtotal
                Dim sngTax As Single = sngSubTotal * sngTaxRate         'calculate the tax, in $s
                Dim sngTotal As Single = sngSubTotal + sngTax           'calculate the total for each purchase

                'dispaly and format the output
                lstOutput.Items.Add(String.Format(fmtStr, strItem, Format(sngPrice, "Currency"), intQuantity, Format(sngSubTotal, "Currency"), Format(sngTax, "Currency"), Format(sngTotal, "Currency")))

            Else
                'tell the user where the error was found
                MsgBox("Error in input file for item " & intLineNumber)

            End If

        End While

        'close the file, we're done reading it
        readerVar.Close()
        readerVar = Nothing
    End Sub

    Private Sub btnExit_Click(sender As System.Object, e As System.EventArgs) Handles btnExit.Click
        'this procedure ends the program
        End
    End Sub

    Private Sub btnClear_Click(sender As System.Object, e As System.EventArgs) Handles btnClear.Click
        'this procedure clears all items from the listbox that the user has purchased
        lstOutput.Items.Clear()
    End Sub
End Class
